package com.qf.hello;

import com.qf.util.ConnUtil;
import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;

public class Consumer {

    @Test
    public void consume() throws IOException {
        //1. 获取连接对象
        Connection connection = ConnUtil.getConnection();

        //2. 创建Channel通道
        Channel channel = connection.createChannel();

        //3. 创建队列
        channel.queueDeclare("hello-queue",true,false,false,null);

        //4. 监听队列中的消息 hello-queue
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                // 当消费者监听到队列中有消息时，会执行handleDelivery方法，处理消息
                System.out.println("消费者获取到消息啦:" + new String(body,"UTF-8"));
            }
        };
        channel.basicConsume("hello-queue",true,consumer);
        System.out.println("开始监听hello-queue");

        System.in.read();
    }


}
